<?php
/*
 * 事件提醒接口
 */
class Apiv1_RemindController extends Ctrl_Apiatk{
    public function indexAction(){
        $p = $_REQUEST;
        $pSId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $tTime = time();

        //$tRMO = new RemindModel;
        $tPMO = new PlanModel;
        $tRPMO = new RemindplanModel;
        $tEMMO = new EventmediaModel;
        $tImgUrl = Yaf_Registry::get("config")->web->url->img; 

        //查询符合当前时间五分钟前的所有提醒事件
        //$tRDatas = $tPMO->field('*')->where('(starttime >= '.$tTime.' or endtime <= '.(strtotime(date('Y-m-d',strtotime('+1 days')))+5*60*60).' and FROM_UNIXTIME(endtime,\'%H:%i\') = \''.date('H:i',$tTime).'\') and status = 2')->fList();
        //$tRDatas1 = $tPMO->field('*')->where('startdate = \''.date('Y-m-d',$tTime).'\' and enddate = \''.date('Y-m-d',$tTime).'\' and endtime > '.$tTime.' and endtime <= '.(strtotime(date('Y-m-d',strtotime('+1 days')))+5*60*60).'and status = 2')->fList();
        //$tSql = 'select * from '.$tRMO->table.' where starttime >= '.$tTime.' and endtime >= '.($tTime+5*60);
        $tSql = 'select * from '.$tPMO->table.' where (startdate = \''.date('Y-m-d',$tTime).'\' and enddate = \''.date('Y-m-d',$tTime).'\' and endtime > '.$tTime.' and endtime < '.(strtotime(date('Y-m-d',strtotime('+1 days')))+5*60*60).') or ( endtime > '.(strtotime(date('Y-m-d',strtotime('+1 days')))+5*60*60).') and status = 2';
        $tRDatas = $tPMO->query($tSql);
        /*$tSql = 'select * from '.$tPMO->table.' where endtime > '.(strtotime(date('Y-m-d',strtotime('+1 days')))+5*60*60).' and status = 2';
        $tRDatas2 = $tPMO->query($tSql);*/
        if(!empty($tRDatas)){
            foreach($tRDatas as $tRow){
                $tResponddate = strtotime(date('Y-m-d',$tTime));
                $tRespondtime = strtotime(date('Y-m-d',$tTime).' '.$tRow['respond_time']);
                $tData = array(
                    'r_id' => $tRow['id'],
                    'sid' => $tRow['sid'],
                    'sw_id' => $tRow['sw_id'],
                    'event_type' => $tRow['event_type'],
                    'status' => 0,
                    'respond_date' => $tResponddate,
                    'respondtime' => $tRespondtime,
                    'created' => $tTime,
                );
                //判断同类型同一时间发生的事件是否存在 
                //$tRPDatas = $tRPMO->field('id')->where('r_id = '.$tRow['id'].' and sid = '.$tRow['sid'].' and sw_id = '.$tRow['sw_id'].' and event_type = '.$tRow['event_type'].' and respondtime = '.$tRespondtime)->fList();
                //$tSql = 'select id from '.$tRPMO->table.' where r_id = '.$tRow['id'].' and sid = '.$tRow['sid'].' and sw_id = '.$tRow['sw_id'].' and event_type = '.$tRow['event_type'].' and respondtime = '.$tRespondtime;
                //$tRPDatas = $tRPMO->field('id')->where('r_id = '.$tRow['id'].' and ')->fRow();
                if($tRow['event_type'] !==9 || $tRow['event_type'] !== 23){
                    $tSql = 'select rp.id id from '.$tRPMO->table.' rp left join '.$tPMO->table.' p on p.id = rp.r_id where rp.r_id = '.$tRow['id'].' and p.status = 2';
                    $tRPDatas = $tRPMO->query($tSql)[0];
                }

                if(empty($tRPDatas['id'])){
                    if(!$tRPMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('系统内部错误');
                    }
                }else{
                    $tData = array(
                        'r_id' => $tRow['id'],
                        'sid' => $tRow['sid'],
                        'sw_id' => $tRow['sw_id'],
                        'event_type' => $tRow['event_type'],
                        'status' => 0,
                        'respond_date' => $tResponddate,
                        'respondtime' => $tRespondtime,
                        'updated' => $tTime,
                        'id' => $tRPDatas['id'],
                    );
                    if(!$tRPMO->update($tData)){
                        Tool_Fnc::ajaxMsg('系统内部错误001');
                    }
                    
                }
            }
        }
    
        //验证事件
        //$tDatas = $tRPMO->field('*')->where('sid = '.$pSId.' and sw_id = '.$this->tUid)->fList();
        $tSql = 'select concat(\''.$tImgUrl.'\',em.audio) audio_url,p.at at,rp.respondtime respondtime,rp.id id,p.plan_type remind_type,p.event_type event_type,rp.sw_id sw_id,rp.respondtime respondtime,p.respond_time respond_time,p.description description,p.remarks remarks from '.$tRPMO->table.' rp left join '.$tPMO->table.' p on p.id = rp.r_id left join '.$tEMMO->table.' em on em.event_type = p.event_type where rp.sid = '.$pSId.' and rp.sw_id = '.$this->tUid.' and rp.respondtime >= '.($tTime-1*60).' and rp.respondtime <= '.$tTime.' and rp.status = 0';
        $tDatas = $tRPMO->query($tSql);
        if(empty($tDatas)){
            Tool_Fnc::ajaxMsg('没有该老人的提醒事件');
        }else{
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
    }
    public function editAction(){
        $p = $_REQUEST;
        $pSId = empty($p['id'])?Tool_Fnc::ajaxMsg('事件ID不能为空'):Tool_Fnc::safe_string($p['id']);

        $tTime = time(); 
        $tData = array(
            'id' => $pSId,
            'status' => 1,
            'updated' => $tTime,
    );
        $tRPMO = new RemindplanModel;
        if(!$tRPMO->update($tData)){
            Tool_Fnc::ajaxMsg('更新失败');
        }
        Tool_Fnc::ajaxMsg('更新成功',1);
    }
    public function listAction(){
        $p = $_REQUEST;
        $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);

        $tRMO = new RemindModel;
        $tPMO  = new PlanModel;
        $tRPMO = new RemindplanModel;

        $tEndtime = strtotime(date('Y-m-d',strtotime('+1 day')))+5*60*60;
        $tStarttime = strtotime(date('Y-m-d'))+5*60*60;

        $tSql = 'select rp.status status,rp.id id,p.plan_type remind_type,p.event_type event_type,rp.sw_id sw_id,rp.respondtime respondtime,p.description description,p.remarks remarks from '.$tRPMO->table.' rp join '.$tPMO->table.' p on p.id = rp.r_id where rp.sid = '.$pSid.' and rp.sw_id = '.$this->tUid.' and rp.respondtime <= '.$tEndtime.' and rp.respondtime >= '.$tStarttime.' order by rp.respondtime desc';

        $tDatas = $tRPMO->query($tSql);

        Tool_Fnc::ajaxMsg('',1,$tDatas);
    }
}
?>
